package com.tongji.seller.utils
{
	import mx.collections.ArrayCollection;

	public class PageTools
	{
		private var startIndex:int = 1; //起始页
		
		private  var  recordCount:Number; //记录总数
		private var groupSize:Number;//每次显示的页数		
		private var pageSize:int = 2; //每页的显示数量
		public var pageIndex:int = 1;//当前页面的索引

		private var _nums:ArrayCollection;//绑定在页码list的数据
		
		public function get nums():ArrayCollection
		{
			return _nums;
		}

		public function set nums(value:ArrayCollection):void
		{
			_nums = value;
		}

 
		public var pageCount:int;//总页数
		public var groupIndex:int = 1; //页数分组
		public var pageArray:ArrayCollection ; //绑定在页码dropdown的数据，总页数
		
		public function PageTools(recordCount:Number, groupSize:Number, pageSize:Number, pageIndex:Number):void
		{

			nums = new ArrayCollection();
			pageArray = new ArrayCollection();
			this.recordCount = recordCount;
			this.groupSize = groupSize;
			this.pageSize = pageSize;
			this.pageIndex = pageIndex;
			
			pageCount= Math.ceil(recordCount / pageSize);//计算总页数
			
			for(var k:int = 1; k <= pageCount; k++)				
			{				
				pageArray.addItem({label : k.toString(), data : k-1});
			}
			
			if( pageIndex % groupSize == 0)
			{
				groupIndex = pageIndex / groupSize;
			}
			else
				groupIndex = pageIndex / groupSize +1;
			
			var minPage:int = (groupIndex - 1) * groupSize +1;
			var maxPage:int = groupIndex*groupSize + 1;
			var i:int = 0;
			if (pageCount <= groupSize )
			{
				for (i = 1; i < pageCount+1; i++)
				{
					nums.addItem(createObjByPageIndex(i));
				}
			}
			else /* (minPage < pageCount && maxPage >= pageCount) */
			{
				if (groupIndex == 1)
				{
					for (i = 1; i < groupSize + 1; i++)
					{
						nums.addItem(createObjByPageIndex(i));
						
					}
					//nums.addItem(createObj("", false, false));
					
				} 
				else if(groupIndex == Math.ceil(pageCount / groupSize))
				{
					//nums.addItem(createObj("", false, false));
					for( minPage; minPage <= pageCount; minPage++)
					{
						nums.addItem(createObjByPageIndex(minPage));
					}
				}
				else
				{
					if (pageIndex > maxPage)
					{
						/* for (i = 0; i < 3; i++)
						{
						nums.addItem(createObjByPageIndex(i));
						} */
						
						//nums.addItem(createObj("", false, false));
						nums.addItem(createObj(pageIndex, true, false));
						nums.addItem(createObj(pageIndex + 1, false, false));
						nums.addItem(createObj(pageIndex + 2, false, false));
						//nums.addItem(createObj("", false, false));
						/* nums.addItem(createObj(pageCount - 1, true, false));
						nums.addItem(createObj(pageCount, true, false)); */
					}
					else if (pageIndex < minPage)
					{
						//nums.addItem(createObj("", false, false));
						nums.addItem(createObj(pageIndex -2, false, false));
						nums.addItem(createObj(pageIndex - 1, false, false));
						nums.addItem(createObj(pageIndex , true, true));
						//nums.addItem(createObj("", false, false));
					}
					else /* if((pageIndex >= minPage) && pageIndex <= maxPage)) */
					{
						nums.addItem(createObj("", false, false));
						for( minPage; minPage < maxPage; minPage++)
						{
							nums.addItem(createObjByPageIndex(minPage));
						}
						//nums.addItem(createObj("", false, false));
					}
				}
				
			}
		}

		private function createObj(number:*, enabled:Boolean, selected:Boolean):Object
		{
			var obj:Object = new Object();
			obj.number = number;
			obj.enabled = enabled;
			obj.selected = selected;
			return obj;
		}
		
		private function createObjByPageIndex(i:int):Object
		{
			var obj:Object = new Object();
			obj.number = i ;
			obj.enabled = true;
			if (i == pageIndex)
				obj.selected = true;
			else
				obj.selected = false;
			return obj;
		}
	}
}